Saving and Loading Libraries of Subroutines
So far, you've seen examples of defining and calling subroutines in the
same script. This is useful for functions that are repeated more than once in
the same script. But you can also write subroutines for generic functions, such as numeric operations, that are useful in many different scripts. To make a subroutine available in any script, save it as a compiled script, and then use
the scripting addition command Load Script to make it available in a particular script. You can use this technique to create libraries of subroutines for use
in many scripts.For example, the following script contains three subroutines:
centimeterConversion
, which converts inches to centimeters;factorial
, which returns the factorial of a number; andmin
, which
returns the smallest number in a list of numbers.
--the centimeterConversion subroutine converts inches to centimeters on centimeterConversion from x if class of x is contained by {integer, real} then return x * 2.54 else error "The parameter must be a real number or an integer." end if end centimeterConversion --the factorial() subroutine returns the factorial of a number on factorial(x) if x > 0 then return x * (factorial(x - 1)) else return 1 end if end factorial --the min() subroutine returns the smallest number in a list on min(numberList) if class of numberList ≠ list or numberList = {} then ÿ return numberList if length of numberList = 1 then return item 1 of numberList copy item 1 of numberList to frontNumber copy length of numberList to listLength copy min(items 2 thru listLength of numberList) to tailNumber if frontNumber > tailNumber then return tailNumber else return frontNumber end if end minTo save this script as a compiled script, choose Save As from the Script Editor's File menu and choose Compiled Script from the Kind pop-up menu. Then save the script as a file called Numeric Operations. (If you are using a different script editor, see the documentation that came with it.)After you save the script as a compiled script, use the Load Script scripting addition command to make the subroutines it contains available in the current script. For example, the Load Script command in the following script assigns the compiled script Numeric Operations to the variable
NumberLib
. To call the subroutines in Numeric Operations, use a Tell statement. The Tell statement in the example calls thefactorial
subroutine. (You must have a compiled script called Numeric Operations in the specified location for this script to work correctly.)
set NumberLib to (load script file "MacHD:Scripts:Numeric Operations") tell NumberLib factorial(10) end tell
- Note
- The Load Script scripting addition command loads the compiled script as a script object. For a definition of Load Script, see the AppleScript Scripting Additions Guide.
![]()
- Script objects are user-defined objects that are treated as values by AppleScript; for more information about them, see Chapter 9, "Script Objects."
![]()